package com.cory.utils.valid;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class RegexUtils {
	//
	private static String IDCARD = "^(\\d{6})(18|19|20)?(\\d{2})([01]\\d)([0123]\\d)(\\d{3})(\\d|X|x)?$";// 简单检查身份证方法";
	private static String EMAIL = "^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*$";// 检查电子邮件
	private static String TEL = "^1[356789]\\d{9}$";// 检查手机号码
	private static String TEL_INNER = "^[19][356789]\\d{9}$";// 检查手机号码
	private static String BIRTHDAY = "^(\\d){4}[/-]{0,1}(\\d){1,2}[/-]{0,1}(\\d){1,2}$";// 检查手机号码
	/** 用户名中的非法字符 */
	private static String USERNAME_INVALID_CHARS = "[!@#$%^&<>?]+";
	
	private static Pattern P_RANGE = Pattern.compile("[0-9]{1,}");
	private static Pattern P_IDCARD = Pattern.compile(IDCARD);
	private static Pattern P_EMAIL = Pattern.compile(EMAIL);
	private static Pattern P_TEL = Pattern.compile(TEL);
	private static Pattern P_TEL_INNER = Pattern.compile(TEL_INNER);
	private static Pattern P_BIRTHDAY = Pattern.compile(BIRTHDAY);
	
	private static Pattern P_USERNAME_INVALID_CHARS = Pattern.compile(USERNAME_INVALID_CHARS);
	
	private static DateFormat dd = new SimpleDateFormat("yyyy/MM/dd");
	static {
		dd.setLenient(false);
	}

	public static boolean isCardId(String idNo) {
		if (StringUtils.isEmpty(idNo)) {
			return false;
		}
		Matcher m = P_IDCARD.matcher(idNo);
		return m.find();
	}

	public static boolean isEmail(String email) {
		if (StringUtils.isEmpty(email)) {
			return false;
		}
		Matcher m = P_EMAIL.matcher(email);
		return m.find();
	}

	public static boolean isTel(String mobile) {
		if (StringUtils.isEmpty(mobile)) {
			return false;
		}
		Matcher m = P_TEL.matcher(mobile);
		return m.find();
	}
	
	public static boolean isTel_Inner(String mobile) {
		if (StringUtils.isEmpty(mobile)) {
			return false;
		}
		Matcher m = P_TEL_INNER.matcher(mobile);
		return m.find();
	}

	/**
	 * 字符串转日期
	 * 
	 * @param dateStr
	 * @param formatStr
	 * @return
	 */
	public static boolean isDate(String dateStr) {
		boolean rs = true;
		try {
			dd.parse(dateStr);
		} catch (ParseException e) {
			rs = false;
		}
		return rs;
	}

	public static boolean isBirthday(String birthday) {
		if (StringUtils.isEmpty(birthday)) {
			return false;
		}
		Matcher m = P_BIRTHDAY.matcher(birthday);
		boolean rs = m.find();
		if (rs) {
			return isDate(birthday);
		} else {
			return false;
		}
	}

	public static boolean range(String period, int min, int max) {
		if (StringUtils.isEmpty(period)) {
			return false;
		}
		period = period.trim();
		Matcher matcher = P_RANGE.matcher((CharSequence)period);
		if(!matcher.matches()){
			return false;
		}
	    Integer value = Integer.parseInt(period);
		if(value < min || value > max){
			return false;
		}else{			
			return true;
		}
	}
	
	/**
	 * 检查用户是否合法
	 * 
	 * @param username
	 * @return
	 */
	public static boolean isValidUsername(String username) {
		if (StringUtils.isEmpty(username)) {
			return false;
		}

		Matcher m = P_USERNAME_INVALID_CHARS.matcher(username);
		if (m.find()) {
			return false;
		}

		return true;
	}

	public static void main(String[] args) {
		/*System.out.println(RegexUtil.isCardId("430124198109252912"));
		System.out.println(RegexUtil.isEmail("1234567@1.com"));
		System.out.println(RegexUtil.isBirthday("1989/12/11"));
		System.out.println(RegexUtil.isTel("13798207201"));
		System.out.println(RegexUtil.range("4",1,12));*/
		System.out.println(RegexUtils.isTel("aadzzzxc"));
	}

}
